<template>
  <a-form class="smart-query-form smart-query-form-max">
    <a-form-item label="关键字" class="smart-query-form-item">
      <a-input v-model:value.trim="queryForm.keyword" placeholder="标题/内容" />
    </a-form-item>

    <a-form-item label="类型" class="smart-query-form-item">
      <!-- <EnumSelect v-model:value="queryForm.type_id" placeholder="消息类型" enum-name="MESSAGE_TYPE_ENUM" /> -->
        <a-select v-model="queryForm.type_id" placeholder="消息类型" allowClear>
            <a-select-option v-for="item in cateList" :key="item.id" @change="quickQuery">
                {{ item.name }}
            </a-select-option>
        </a-select>
    </a-form-item>

    <a-form-item label="消息时间" class="smart-query-form-item">
      <a-space direction="vertical" :size="12">
        <a-range-picker v-model:value="searchDate" @change="dateChange" />
      </a-space>
    </a-form-item>

    <a-form-item label="已读" class="smart-query-form-item">
      <a-radio-group v-model:value="is_read" @change="quickQuery">
        <a-radio-button :value="null">全部</a-radio-button>
        <a-radio-button :value="0">未读</a-radio-button>
        <a-radio-button :value="1">已读</a-radio-button>
      </a-radio-group>
    </a-form-item>

    <a-form-item class="smart-query-form-item smart-margin-left10">
      <a-button-group>
        <a-button type="primary" @click="quickQuery">
          <template #icon>
            <SearchOutlined />
          </template>
          查询
        </a-button>
        <a-button @click="resetQuery">
          <template #icon>
            <ReloadOutlined />
          </template>
          重置
        </a-button>
      </a-button-group>
    </a-form-item>
  </a-form>

  <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="messageId" :pagination="false" bordered>
    <template #bodyCell="{ text, record, column }">
      <template v-if="column.dataIndex === 'type_id'">
        <span>{{ getDescByValue('MESSAGE_TYPE_ENUM', text) }}</span>
      </template>
      <template v-if="column.dataIndex === 'reads[0][is_read]'">
        <span v-show="record.reads[0].is_read">已读</span>
        <span v-show="!record.reads[0].is_read" style="color: red">未读</span>
      </template>
      <template v-if="column.dataIndex === 'title'">
        <span v-show="record.reads[0].is_read">
          <a @click="toDetail(record)" style="color: #8c8c8c"
            >【{{ getDescByValue('MESSAGE_TYPE_ENUM', record.type_id) }}】{{ text }}</a
          >
        </span>
        <span v-show="!record.reads[0].is_read">
          <a @click="toDetail(record)">【{{ getDescByValue('MESSAGE_TYPE_ENUM', record.type_id) }}】{{ text }} </a>
        </span>
      </template>
    </template>
  </a-table>

  <div class="smart-query-table-page">
    <a-pagination
      showSizeChanger
      showQuickJumper
      show-less-items
      :pageSizeOptions="PAGE_SIZE_OPTIONS"
      :defaultPageSize="queryForm.limit"
      v-model:current="queryForm.page"
      v-model:pageSize="queryForm.limit"
      :total="total"
      @change="ajaxQuery"
      @showSizeChange="ajaxQuery"
      :show-total="(total) => `共${total}条`"
    />
  </div>

  <MessageDetail ref="messageDetailRef" @refresh="ajaxQuery" />
</template>
<script setup lang="ts">
  import { reactive, ref, onMounted } from 'vue';
  import { messageApi } from '@/api/support/message-api';
  import { PAGE_SIZE,PAGE_SIZE_OPTIONS } from '@/constants/system/common-const';
  import EnumSelect from '@/components/framework/enum-select/index.vue';
  import { errorSentry } from '@/lib/error-sentry';
  import MessageDetail from './components/message-detail.vue';
  import { useEnums } from '@/hooks/plugins/useEnums'

  const { getDescByValue } = useEnums()
    const cateList = ref([]);
  const columns = reactive([
    {
      title: '消息',
      dataIndex: 'title',
    },
    {
      title: '状态',
      width: 80,
      dataIndex: 'reads[0][is_read]',
    },
    {
      title: '时间',
      dataIndex: 'create_time',
      width: 180,
    },
  ]);
const is_read = ref(null)
  const queryFormState = {
    keyword: '',
    type_id: null,
    // dataId: null,
    // readFlag: null,
    end_time: null,
    start_time: null,
    page: 1,
    limit: PAGE_SIZE,
    // searchCount: true,
    // receiverType: null,
    // receiverId: null,
  };
  const queryForm = reactive({ ...queryFormState });
  const tableLoading = ref(false);
  const tableData = ref([]);
  const total = ref(0);

  // 日期选择
  let searchDate = ref();

  function dateChange(dates, dateStrings) {
    queryForm.start_time = dateStrings[0];
    queryForm.end_time = dateStrings[1];
  }

  function resetQuery() {
    searchDate.value = [];
    Object.assign(queryForm, queryFormState);
    ajaxQuery();
  }

  function quickQuery() {
    queryForm.page = 1;
    ajaxQuery();
  }

  // 查询
  async function ajaxQuery() {
    try {
      tableLoading.value = true;
      const res = await messageApi.queryCategory();
      cateList.value = res.data;
      let responseModel = await messageApi.queryMessage({...queryForm, is_read: is_read.value});
      const list = responseModel.data.data;
      total.value = responseModel.data.total;
      tableData.value = list;
    } catch (e) {
      errorSentry.captureError(e);
    } finally {
      tableLoading.value = false;
    }
  }

  // -------------------- 详情 -----------------------------------

  const messageDetailRef = ref();

  function toDetail(message) {
    messageDetailRef.value.show(message);
  }

  onMounted(ajaxQuery);
</script>
